package com.ljm.commom.web.conf;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ljm.commom.web.conf.properties.ApiSecurityProperties;
import com.ljm.commom.web.conf.properties.OutJoinSysUrlProperties;
import com.ljm.commom.web.filter.ApiSecurityFilter;
import com.ljm.commom.web.filter.BodyReaderRequestFilter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 版权：    Copyright by ljm
 * 描述：
 * 修改人：  HuamingChen
 * 修改时间：2020/3/6
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
@Configuration
@EnableConfigurationProperties({ApiSecurityProperties.class, OutJoinSysUrlProperties.class} )
public class FilterConf {
    private final ApiSecurityProperties apiSecurityProperties;
    private final OutJoinSysUrlProperties outJoinSysUrlProperties;

    public FilterConf(ApiSecurityProperties apiSecurityProperties,OutJoinSysUrlProperties outJoinSysUrlProperties){
        this.apiSecurityProperties=apiSecurityProperties;
        this.outJoinSysUrlProperties=outJoinSysUrlProperties;
    }

    @Bean
    public FilterRegistrationBean<BodyReaderRequestFilter> bodyReaderRequestFilter() {
        FilterRegistrationBean<BodyReaderRequestFilter> bodyReaderRequestFilterBean = new FilterRegistrationBean<>();
        bodyReaderRequestFilterBean.setFilter(new BodyReaderRequestFilter());
        bodyReaderRequestFilterBean.addUrlPatterns("/*");
        bodyReaderRequestFilterBean.setName("bodyReaderRequestFilter");
        bodyReaderRequestFilterBean.setOrder(0);
        return bodyReaderRequestFilterBean;
    }

    @Bean
    public FilterRegistrationBean<ApiSecurityFilter> apiSecurityFilter() throws JsonProcessingException {
        FilterRegistrationBean<ApiSecurityFilter> apiSecurityFilter = new FilterRegistrationBean<>();
        apiSecurityFilter.setFilter(new ApiSecurityFilter());
        apiSecurityFilter.addUrlPatterns("/*");
        apiSecurityFilter.setName("apiSecurityFilter");
        apiSecurityFilter.setOrder(1);
        apiSecurityFilter.addInitParameter("outJoinSysUrl", JSON.toJSONString(outJoinSysUrlProperties.getOutJoinSysUrl()));
        apiSecurityFilter.addInitParameter("apiSecurityProperties",JSON.toJSONString(apiSecurityProperties));
        return apiSecurityFilter;
    }
}
